<?php


namespace app\admin\business;


use app\common\model\mysql\AdminUser as AdminUserModel;

class AdminUser
{
    public static function login($data)
    {

        try {
            $user = (new AdminUserModel())->getAdminUserByUsername($data['username']);

            if (is_null($user) || $user->status != 1) {
                throw  new \Exception('用户不存在');
            }

            if ($user->password != md5($data['password'])) {
                throw  new \Exception('密码不正确');
            }

            //更新用户
            $update_date = [
                'last_login_time' => time(),
                'last_login_ip' => request()->ip(),
            ];

            if (empty((new AdminUserModel())->updateById($user->id, $update_date))) {
                throw  new \Exception('更新失败');
            }
        } catch (\Exception $e) {
            throw  new \Exception($e->getMessage());
        }
        //记录session
        session('session_admin', $data['username']);
        return true;
    }
}